package com.corx.network.core.util;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author gx
 * @version 1.0.0
 */
@Slf4j
public class JdbcUtils {
    /**
     * 安全绑定 PreparedStatement 参数，自动处理常见类型
     * @param ps PreparedStatement 对象
     * @param paramIndex 参数索引（从1开始）
     * @param value 参数值
     */
    public static void setParameter(PreparedStatement ps, int paramIndex, Object value) throws SQLException {
        if (value == null) {
            ps.setNull(paramIndex, java.sql.Types.NULL);
            return;
        }

        if (value instanceof String) {
            ps.setString(paramIndex, (String) value);
        } else if (value instanceof Integer) {
            ps.setInt(paramIndex, (Integer) value);
        } else if (value instanceof Long) {
            ps.setLong(paramIndex, (Long) value);
        } else if (value instanceof Double) {
            ps.setDouble(paramIndex, (Double) value);
        } else if (value instanceof Boolean) {
            ps.setBoolean(paramIndex, (Boolean) value);
        } else if (value instanceof BigDecimal) {
            ps.setBigDecimal(paramIndex, (BigDecimal) value);
        } else if (value instanceof Date) {
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(((Date) value).getTime()));
        } else if (value instanceof java.time.LocalDateTime) {
            ps.setTimestamp(paramIndex, java.sql.Timestamp.valueOf((java.time.LocalDateTime) value));
        } else {
            // 复杂对象必须提前序列化为字符串
            ps.setString(paramIndex, value.toString());
        }
    }

    public static String toSqlType(String fieldType) {
        switch (fieldType.toLowerCase()) {
            case "string":
                return "VARCHAR(255)";
            case "int":
            case "integer":
                return "INT";
            case "long":
                return "BIGINT";
            case "float":
            case "double":
                return "DOUBLE";
            case "boolean":
            case "bool":
                return "TINYINT(1)";
            default:
                return "VARCHAR(255)";
        }
    }

    /**
     * 类型转换：string/int/long/float/double/boolean
     */
    public static Object toJavaType(Object value, String fieldType) {
        if (value == null) {
            return null;
        }
        try {
            String type = fieldType.toLowerCase();
            switch (type) {
                case "string":
                    return value.toString();
                case "int":
                case "integer":
                    return Integer.parseInt(value.toString());
                case "long":
                    return Long.parseLong(value.toString());
                case "float":
                    return Float.parseFloat(value.toString());
                case "double":
                    return Double.parseDouble(value.toString());
                case "boolean":
                case "bool":
                    return Boolean.parseBoolean(value.toString());
                default:
                    return null;
            }
        } catch (Exception e) {
            log.warn("字段值 [{}] 转换类型 [{}] 失败", value, fieldType);
            return null;
        }
    }
}
